package org.karelun.whiteboard.ui.local;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: KarelunHuang
 * @Date: Feb 22, 2013
 **/
public class JavaDebug {
	
	public static int getPid() {
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        String name = runtime.getName(); // format: "pid@hostname"
        try {
            return Integer.parseInt(name.substring(0, name.indexOf('@')));
        } catch (Exception e) {
            System.err.println(e.getMessage());
            return -1;
        }
    }

    public static void printEnv(){
        Map<String,String> map = System.getenv();
        Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
        Map.Entry<String, String> entry;
        while (iter.hasNext()) {
            entry = iter.next();
            String name = entry.getKey();
            String value = entry.getValue();

            System.out.println(name + ": "+ value);
        }
    }

    public static void printProperties(){
        Properties pros = System.getProperties();
        pros.list(System.out);
    }

    public static String getPWD(){
        return System.getProperty("user.dir");
    }

    public static void printSimpleMemoryInfo(){
        Runtime runTime = Runtime.getRuntime();
        System.out.println("Total Mem:" + runTime.totalMemory() + " Max Mem:" +runTime.maxMemory()
                + " Free mem:" + runTime.freeMemory() );
    }

    public static void printMemoryInfo(String info){
        if( info != null){
            System.out.println(info+ " JavaDebug:printMemoryInfo");
        }

        printSimpleMemoryInfo();
        ExecCommand("jmap " + getPid());
    }

    public static void ExecCommand(String cmd){
        System.out.println("Exec command: "+cmd);

        try{
            Process pro = null;
            Runtime runTime = Runtime.getRuntime();
            if (runTime == null) {
                System.err.println("Create runtime false!");
                throw new RuntimeException("Create runtime false!");
            }

            pro = runTime.exec(cmd);
            BufferedReader input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
            PrintWriter output = new PrintWriter(new OutputStreamWriter(pro.getOutputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            input.close();
            output.close();
            pro.destroy();
        }
        catch(IOException e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("run command error!");
        }

        System.out.println("Exec command: "+cmd + " finish.");
    }


    public static void main(String[] args) throws Exception {
        printProperties();

        //printEnv();

        System.out.println("========================");
        int pid = getPid();
        System.out.println("pid: " + pid);
        System.out.println("pwd: " + getPWD());

        printMemoryInfo(null);

        ExecCommand("ls -l");
        ExecCommand("jmap " + pid);

        printMemoryInfo(null);
    }
}
